package com.shiwaixiangcun.customer.ui.activity;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mobstat.SendStrategyEnum;
import com.baidu.mobstat.StatService;
import com.shiwaixiangcun.customer.GlobalApi;
import com.shiwaixiangcun.customer.R;
import com.shiwaixiangcun.customer.base.BaseActivity;
import com.shiwaixiangcun.customer.entity.ResponseEntity;
import com.shiwaixiangcun.customer.ui.IDetailView;
import com.shiwaixiangcun.customer.ui.dialog.ShareDialog;
import com.shiwaixiangcun.customer.utils.SdCordUtil;
import com.shiwaixiangcun.customer.utils.Utils;
import com.shiwaixiangcun.customer.widget.ChangeLightImageView;
import com.shiwaixiangcun.customer.widget.X5WebView;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

/**
 * @author Administrator
 */
public class SurroundDetailsActivity extends BaseActivity implements View.OnClickListener, IDetailView {

    private ChangeLightImageView backLeft;
    private ImageView ivShareRight;
    private X5WebView webView;
    private String articleId;
    private StringBuilder urlWeb = new StringBuilder();
    private StringBuilder urlShare = new StringBuilder();
    private String detailTitle;
    private TextView tvPageName;
    private TextView tvTop;
    private String detailContent;
    private String shareImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        Resources res = getResources();
        Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.start_page);
        shareImage = SdCordUtil.saveMyBitmap("shareImage", bmp);
        //        百度统计
        StatService.setLogSenderDelayed(10);
        StatService.setSendLogStrategy(this, SendStrategyEnum.APP_START, 1, false);
        StatService.setSessionTimeOut(30);

        Intent intent = getIntent();
        articleId = intent.getStringExtra("articleId");
        detailTitle = intent.getStringExtra("detailTitle");
        detailContent = intent.getStringExtra("detailContent");
        layoutView();
        initData();

        webView = findViewById(R.id.webview);
        //设置WebView属性，能够执行Javascript脚本
        webView.getSettings().setJavaScriptEnabled(true);
        //加载需要显示的网页
        Log.e("ss", urlWeb.toString());
        webView.loadUrl(urlWeb.toString());
        //设置Web视图
        webView.setWebViewClient(new HelloWebViewClient());

        webView.setOnScrollChangeListener(new X5WebView.OnScrollChangeListener() {
            @Override
            public void onPageEnd(int l, int t, int oldl, int oldt) {

            }

            @Override
            public void onPageTop(int l, int t, int oldl, int oldt) {

            }

            @Override
            public void onScrollChanged(int l, int t, int oldl, int oldt) {

            }

            @Override
            public void scrollDistance(float distant) {
                if (distant > 300) {
                    if (Utils.isNotEmpty(detailTitle)) {
                        tvPageName.setText(detailTitle);
                        tvTop.setVisibility(View.VISIBLE);
                    }

                } else {
                    tvPageName.setText("");
                    tvTop.setVisibility(View.GONE);
                }

            }
        });



    }

    private void layoutView() {
        backLeft = findViewById(R.id.back_left);
        ivShareRight = findViewById(R.id.iv_share_right);
        tvTop = findViewById(R.id.tv_top);
        tvPageName = findViewById(R.id.tv_page_name);
        tvPageName.setEllipsize(TextUtils.TruncateAt.END);
        tvPageName.setLines(1);
    }

    private void initData() {
        urlWeb.append(GlobalApi.INSTANCE.getDOMAIN_PM()).append("/mi/article/detailView.htm").append("?articleId=").append(articleId).append("&app=true");
        urlShare.append(GlobalApi.INSTANCE.getDOMAIN_PM()).append("/mi/article/detailView.htm").append("?articleId=").append(articleId);
        ivShareRight.setVisibility(View.VISIBLE);
        backLeft.setOnClickListener(this);
        ivShareRight.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.back_left:
                finish();
                break;
            case R.id.iv_share_right:
                showShare();
                break;
            default:
                break;
        }
    }


    @Override
    public void setBgaAdpaterAndClickResult(ResponseEntity result) {

    }

    private void showShare() {

        ShareDialog shareDialog = new ShareDialog();
        Bundle bundle = new Bundle();
        bundle.putString("title", detailTitle);
        bundle.putString("titleUrl", urlShare.toString());
        bundle.putString("text", detailContent);
        bundle.putString("imageUrl", shareImage);
        bundle.putString("siteUrl", urlShare.toString());
        shareDialog.setArguments(bundle);
        shareDialog.show(this.getFragmentManager(), "shareDialog");

    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPause(this);
    }

    /**
     *
     */
    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}
